/* $Id: str_passwd.c,v 1.4 2003/11/10 15:08:52 sby Exp $ */

#include "dao.h"

#include <sys/types.h>
#include <unistd.h>

#ifndef	PASSLEN
#define	PASSLEN 35
#endif


/* ----------------------------------------------------- */
/* password encryption					 */
/* ----------------------------------------------------- */


char *crypt();
static char pwbuf[PASSLEN];


char *
genpasswd(pw)
  char *pw;
{
  char saltc[8];
  int i, c;

  if (!*pw)
    return pw;

  srandomdev();

  for (i = 0; i < 8; i++)
    saltc[i] = random() & 077;

  for (i = 0; i < 8; i++)
  {
    c = saltc[i] + '.';
    if (c > '9')
      c += 7;
    if (c > 'Z')
      c += 6;
    saltc[i] = c;
  }
  strlcpy(pwbuf, pw, sizeof(pwbuf));

  crypt_set_format("md5");
  return crypt(pwbuf, saltc);
}


int
checkpasswd(passwd, test)
  char *passwd, *test;
{
  char *pw;

  strncpy(pwbuf, test, PASSLEN);
  pw = crypt(pwbuf, passwd);
  return (strncmp(pw, passwd, PASSLEN));
}

